package com.zhixun.mobile.modules.jiujiudevice;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boxin.ims.modules.jiujiudevice.entity.JiuCashRecord;
import com.boxin.ims.modules.jiujiudevice.entity.JiuDevice;
import com.boxin.ims.modules.jiujiudevice.entity.JiuInk;
import com.boxin.ims.modules.jiujiudevice.entity.JiuInkRecord;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderRecord;
import com.boxin.ims.modules.jiujiudevice.entity.JiuSn;
import com.boxin.ims.modules.jiujiudevice.service.JiuCashConfigService;
import com.boxin.ims.modules.jiujiudevice.service.JiuCashRecordService;
import com.boxin.ims.modules.jiujiudevice.service.JiuDeviceService;
import com.boxin.ims.modules.jiujiudevice.service.JiuInkRecordService;
import com.boxin.ims.modules.jiujiudevice.service.JiuInkService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderLogService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderRecordService;
import com.boxin.ims.modules.jiujiudevice.service.JiuSnService;
import com.boxin.ims.modules.wechat.entity.JiuWechatMember;
import com.boxin.ims.modules.wechat.service.JiuWechatMemberService;
import com.boxin.ims.modules.wechat.service.MobileMessageRecorderService;
import com.boxin.ims.modules.wechat.service.WXPayService;
import com.boxin.ims.modules.wechat.service.WeChatService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.JedisUtils;
import com.thinkgem.jeesite.common.utils.RequestUtil;
import com.zhixun.mobile.modules.base.web.BaseController;
import com.zhixun.mobile.modules.jiujiudevice.common.JsonResult;
import com.zhixun.mobile.modules.jiujiudevice.common.ResultCode;
import com.zhixun.mobile.modules.jiujiudevice.device.model.DataModel;
import com.zhixun.mobile.modules.jiujiudevice.device.model.InkModel;

/**
 * 测试时 开放接口
 * 
 * @author dongchao
 *
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH + "/jiujiudevice")
public class JiuJiuTempFrontController extends BaseController {

	private Logger log = LoggerFactory.getLogger(JiuJiuTempFrontController.class);

	public static final Long USER_ID = 2L;

	@Autowired
	private WXPayService wxPayService;
	@Autowired
	private WeChatService weChatService;
	/**
	 * 微信用户member
	 */
	@Autowired
	private JiuWechatMemberService jiuWechatMemberService;
	/**
	 * 押金记录
	 */
	@Autowired
	private JiuCashRecordService jiuCashRecordService;

	@Autowired
	private MobileMessageRecorderService mobileMessageRecorderService;
	@Autowired
	private JiuOrderRecordService jiuOrderRecordService;
	@Autowired
	private JiuOrderLogService jiuOrderLogService;

	@Autowired
	private JiuInkRecordService jiuInkRecordService;
	/**
	 * 押金全局配置
	 */
	@Autowired
	private JiuCashConfigService jiuCashConfigService;

	@Autowired
	private JiuDeviceService jiuDeviceService;
	@Autowired
	private JiuInkService jiuInkService;

	/**
	 * 测试时方法 上线后需要去掉
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "skipWxNotifyForCash")
	public String skipWxNotifyForCash(HttpServletRequest request, HttpServletResponse response, Model model)
			throws Exception {

		String orderNo = RequestUtil.getString(request, "orderNo");
		JiuCashRecord order = jiuCashRecordService.findByOrderNo(orderNo);
		order.setStatus(JiuCashRecord.STATUS_YES_PAY);
		order.setPayStatus(JiuCashRecord.PAY_STATUS_SUCCESS);
		order.setPayTime(new Date());
		jiuCashRecordService.save(order);

		return JsonResult.Result(ResultCode.SUCCESS, "已成功跳过支付，此为临时接口");
	}

	@ResponseBody
	@RequestMapping(value = "skipWxNotify")
	public String skipWxNotify(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		String orderNo = RequestUtil.getString(request, "orderNo");
		JiuOrderRecord order = jiuOrderRecordService.getByOrderNo(orderNo);
		order.setStatus(JiuOrderRecord.STATUS_YES_PAY);
		order.setPayTime(new Date());
		jiuOrderRecordService.save(order);

		return JsonResult.Result(ResultCode.SUCCESS, "已成功跳过支付，此为临时接口");
	}

	/** 从缓存获取会员 */
	public JiuWechatMember getMember(Long uid, String openId) {

		JiuWechatMember member = (JiuWechatMember) CacheUtils.get(openId);
		if (member == null) {
			member = jiuWechatMemberService.getByUserIdAndOpenId(uid, openId);
			CacheUtils.put(openId, member);
		}
		return member;
	}

	/***
	 * http://localhost/jiujiudevice/f/jiujiudevice/open 测试打开接口
	 */
	@ResponseBody
	@RequestMapping(value = "open")
	public String openDevice() {
		JedisUtils.set("status", "1", 0);
		return JsonResult.Result(ResultCode.SUCCESS, "打开设备");
	}

	@ResponseBody
	@RequestMapping(value = "close")
	public String closeDevice() {
		String sn = "869696043668891";
		List<InkModel> someink = (List<InkModel>) JedisUtils.getObject(sn);
		//获得八路的总消费重量
		double[] d = new double[10];	
		if (someink != null && someink.size() > 0) {
			for (int i = 0; i < someink.size(); i++) {
			     //设置各路的信息 DataModel格式的json
			     // 这个LIST是上一次的 8 路数据
					List<DataModel> dataList3 = someink.get(someink.size()-2).getDatalist();
					// 这个是当前的8路数据
					List<DataModel> dataList4 = someink.get(someink.size()-1).getDatalist();
					// 用上一次的8路数据 分别减去当前的8路数据
					chuliData(dataList3, dataList4, d);
					for (int x = 0; x < someink.size(); x++) {
						   //把各路的总使用重量获得 ，分别存储各路的单价，使用的金额，当前的重量
						   System.out.println("相邻的值----第"+(x+1)+"路的值  = "+d[x]);
						   for (int j = 0; j < dataList4.size(); j++) {
						    DataModel data = dataList4.get(j);
							JiuInk ink  = jiuInkService.getBySnAndName(sn, j+1);
							data.setFlag(ink.getColor());
							data.setPrice(ink.getPrice());
							data.setUseValue(d[x]);
							//g转变为kg
							data.setUseMoney(data.getPrice() * (data.getUseValue()/1000));
						}
				    }
					
					
					if ((i + 1) < (someink.size())) {
						// 这个LIST是上一次的 8 路数据
						List<DataModel> dataList1 = someink.get(i).getDatalist();
						// 这个是当前的8路数据
						List<DataModel> dataList2 = someink.get(i+1).getDatalist();

						// 用上一次的8路数据 分别减去当前的8路数据
						chuliData(dataList1, dataList2, d);

					}
				}
			
			
			
		  } 
		     for (int x = 0; x < someink.size(); x++) {
			   //把各路的总使用重量获得 ，分别存储各路的单价，使用的金额，当前的重量
			   System.out.println("总值----第"+(x+1)+"路的值  = "+d[x]);
		     }
//		     //设置数据库数据
//		    	 for (int i = 0; i < someink.size(); i++) {
//						//存储设备记录表中
//						JiuInkRecord jiuInkRecord  = new JiuInkRecord();
//						
//						jiuInkRecord.setInkStatus(someink.get(i).getInkStatus());
//						jiuInkRecord.setSn(someink.get(i).getSn());
//						//获得设备的名称
//						JiuDevice jiuDevice = jiuDeviceService.getBySn(jiuInkRecord.getSn());
//						String snName = jiuDevice.getName();
//						jiuInkRecord.setSnName(snName);
//						//设置设备状态为关闭状态
//						jiuInkRecord.setSnStatus("0");
//						jiuInkRecord.setInkMessage(com.alibaba.fastjson.JSON.toJSONString(someink.get(i).getDatalist()));
//						jiuInkRecordService.save(jiuInkRecord);
//		    	 }
		JedisUtils.del(sn);
		JedisUtils.del("status");
		return JsonResult.Result(ResultCode.SUCCESS, "关闭设备");
	}

	public static double[] chuliData(List<DataModel> dataList1, List<DataModel> dataList2, double[] d) {
		// 这个 dataList1.size() 就是设备一共有几路
		for (int i = 0; i < dataList1.size(); i++) {
			// 这是计算每一路的 相邻的数据查 如果后者比前者大 不计算
			if (dataList1.get(i).getValue() > dataList2.get(i).getValue()) {

				// 如果d[i] == 0说明就是某一路的初始值
				if (0 == d[i]) {
					d[i] = dataList1.get(i).getValue() - dataList2.get(i).getValue();
				} else {
					d[i] += dataList1.get(i).getValue() - dataList2.get(i).getValue();
				}

			}
		}
		return d;
	}

}
